
package com.szholly.pro.watergas_server.common.util;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import net.sf.json.JSONObject;


public class HttpClientHelper {
	
	/**
	 * http url请求，返回json
	 * @param url
	 * @param jsonParam
	 * @param noNeedResponse
	 * @return
	 */
	public static JSONObject execHttpUrlJSON(String url,List<BasicNameValuePair> jsonParam, boolean noNeedResponse){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		JSONObject jsonResult = null;
	    try {
            if (null != jsonParam) {
                //解决中文乱码问题
            	UrlEncodedFormEntity entity = new UrlEncodedFormEntity(jsonParam, "UTF-8");
            	url += "?" + EntityUtils.toString(entity);
            }
            url = URLDecoder.decode(url, "UTF-8");
            HttpPost httpPost = new HttpPost(url);
            CloseableHttpResponse response = httpClient.execute(httpPost);
            
            /**请求发送成功，并得到响应**/
            if (response.getStatusLine().getStatusCode() == 200) {
                String str = "";
                try {
                    /**读取服务器返回过来的json字符串数据**/
                    str = EntityUtils.toString(response.getEntity());
                    if (noNeedResponse) {

                        return null;

                    }
                    /**把json字符串转换成json对象**/
                    jsonResult = JSONObject.fromObject(str);
                } catch (Exception e) {

                }finally{
                	response.close();
                }

            }
        
        } catch (IOException e) {


        }
        return jsonResult;
	}
	
	
	/**
	 * http post请求，返回json
	 * 张文成
	 * 2017年01月06日
	 */
	@SuppressWarnings("deprecation")
	public static JSONObject execHttpPostJSON(String url,String contentType,String requestData)
	{   
		String result = "";
		JSONObject jsonResult = null;
		try{
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(url); 
			post.setRequestHeader("Content-Type", contentType);
			post.setRequestBody(requestData);
			client.executeMethod(post);
			result = new String(post.getResponseBodyAsString().getBytes("gbk")); 
			jsonResult = JSONObject.fromObject(result);
			post.releaseConnection();
		}catch (Exception e) {
		}
		return jsonResult;
	}
	
	/**
	 * http post请求，返回String
	 * 张文成
	 * 2017年01月06日
	 */
	@SuppressWarnings("deprecation")
	public static String execHttpPostString(String url,String contentType,String requestData)
	{   
		String result = "";
		try{
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(url); 
			post.setRequestHeader("Content-Type", contentType);
			post.setRequestBody(requestData);
			client.executeMethod(post);
			result = new String(post.getResponseBodyAsString().getBytes("gbk")); 
			post.releaseConnection();
		}catch (Exception e) {
		}
		return result;
	}

}